<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="班级" prop="classesId">
        <el-select v-model="queryParams.classesId" @change="changeStudentList($event)" placeholder="请选择班级" :style="{width: '100%'}" >
          <el-option v-for="(item, index) in classesList" :key="index" :label="item.className"
                     :value="item.id" ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="学员" prop="userId">
        <el-select v-model="queryParams.userId" placeholder="选择学员" :style="{width: '100%'}" >
          <el-option v-for="(item, index) in studentList" :key="index" :label="item.name"
                     :value="item.id" ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['teaching:interview_mastery:add']"
        >新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['teaching:interview_mastery:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['teaching:interview_mastery:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="interview_masteryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="学员" align="center" prop="studentName" />
      <el-table-column label="技能平均得分" align="center" prop="avgScore" >
        <template slot-scope="scope">
          {{scope.row.avgScore}}/10
        </template>
      </el-table-column>
      <el-table-column label="沟通得分" align="center" prop="communicationScore">
        <template slot-scope="scope">
          {{scope.row.communicationScore}}/7
        </template>
      </el-table-column>
      <el-table-column label="沟通情况" align="center" prop="communicationInterview_mastery">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ya_communication_mastery" :value="scope.row.communicationMastery ? scope.row.communicationMastery.split(',') : []"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="是否总结" align="center" prop="communicationInterview_mastery">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ya_summarized" :value="scope.row.summarized"/>
        </template>
      </el-table-column>
      <el-table-column label="创建者" align="center" prop="createBy" />
      <el-table-column label="日期" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['teaching:interview_mastery:edit']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['teaching:interview_mastery:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改面试掌握情况记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" fullscreen >
      <el-tabs type="border-card">
        <el-tab-pane label="面试题掌握情况">
          <el-form v-if="title!='修改面试掌握情况记录'"  :model="queryInterviewParams" ref="queryInterviewForm" size="small" :inline="true"  label-width="68px">
            <el-form-item label="班级" prop="classesId">
              <el-select v-model="queryInterviewParams.classesId" clearable @change="changeStudentList($event)" placeholder="请选择班级" :style="{width: '100%'}" >
                <el-option v-for="(item, index) in classesList" :key="index" :label="item.className"
                           :value="item.id" ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="学员" prop="userId">
              <el-select @change="getMasteryDegreeList" v-model="queryInterviewParams.userId" clearable placeholder="学员" :style="{width: '100%'}" >
                <el-option v-for="(item, index) in studentList" :key="index" :label="item.name"
                           :value="item.id" :disabled="item.disabled"></el-option>
              </el-select>
            </el-form-item>
            <el-divider></el-divider>
            <div v-show="queryInterviewParams.userId">
              <el-form-item label="关键字" prop="question">
                <el-input
                  v-model="queryInterviewParams.question"
                  placeholder="请输入面试题"
                  clearable
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="阶段" prop="stage">
                <el-select v-model="queryInterviewParams.stage"  @change="getMasteryDegreeList" placeholder="请选择阶段" clearable>
                  <el-option
                    v-for="dict in dict.type.sys_stage"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
              <el-form-item label="重要性" prop="importance" >
                <el-select v-model="queryInterviewParams.importance" @change="getMasteryDegreeList" placeholder="请选择重要性" clearable>
                  <el-option
                    v-for="dict in dict.type.ya_importance"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
              <el-form-item label="及格" prop="importance" >
                <el-select v-model="queryInterviewParams.qualified"  @change="getMasteryDegreeList" placeholder="请选择阶段" clearable>
                  <el-option
                    v-for="dict in dict.type.ya_qualified"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
              <el-form-item label="上一次" title="只看上一次回答情况" prop="importance" >
                <el-radio v-model="queryInterviewParams.onlyLast" @change="getMasteryDegreeList" :label="true">是</el-radio>
                <el-radio v-model="queryInterviewParams.onlyLast" @change="getMasteryDegreeList" :label="false">否</el-radio>
              </el-form-item>

              <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="getMasteryDegreeList">搜索面试记录</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetMasteryDegreeQuery">重置</el-button>
              </el-form-item>
            </div>
          </el-form>
          <el-table v-show="queryInterviewParams.userId" :data="interview_masteryDegreeList" v-loading="masteryDegreeListLoading">
            <el-table-column label="阶段" align="center"  prop="stage">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.sys_stage" :value="scope.row.stage"/>
              </template>
            </el-table-column>
            <el-table-column label="面试题" align="center"   prop="question" />
            <el-table-column label="重要性" align="center"  prop="importance">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.ya_importance" :value="scope.row.importance"/>
              </template>
            </el-table-column>
            <el-table-column label="上次提问人" align="center"  prop="lastCreateBy"/>
            <el-table-column label="上次掌握程度" align="center" prop="hisDegreeList">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.ya_mastery_degree" :value="scope.row.hisDegree"/>
              </template>
            </el-table-column>
            <el-table-column label="上次得分" align="center"  prop="hisDegreeList">
              <template slot-scope="scope">
                {{scope.row.score}}/10
              </template>
            </el-table-column>
            <el-table-column  label="参考答案" type="expand" >
              <template slot-scope="scope" >
                <mavon-editor v-model="scope.row.answer" :subfield="false" defaultOpen="preview" :editable="false" :toolbarsFlag="false"/>
              </template>
            </el-table-column>
            <el-table-column label="总结" align="center"   prop="summarize" >
              <template slot-scope="scope" >
                <span v-if="scope.row.summarize=='1'">
                  已总结
                </span>
                <span v-else>未总结</span>
              </template>
            </el-table-column>
            <el-table-column label="掌握程度" align="left"  prop="degree" v-if="title!='修改面试掌握情况记录'">
              <template slot-scope="scope">
                <el-checkbox-group  v-model="selectedInterviewList">
                  <el-checkbox
                    v-for="dict in dict.type.ya_mastery_degree"
                    :key="scope.row.id+ ','   +dict.value"
                    :label="scope.row.id+ ','   +dict.value" :disabled="checkDisabled(scope.row.id,dict.value)">
                    {{dict.label}}
                  </el-checkbox>
                </el-checkbox-group>
              </template>
            </el-table-column>
          </el-table>
          <!-- 新增分页组件 -->
          <pagination
            v-show="interviewTotal>0"
            :total="interviewTotal"
            :page.sync="queryInterviewParams.pageNum"
            :limit.sync="queryInterviewParams.pageSize"
            @pagination="getMasteryDegreeList"
          />
        </el-tab-pane>
        <el-tab-pane label="沟通情况/备注">
          <el-form  ref="form" :model="form"  label-width="80px">
            <el-form-item label="沟通情况" prop="communicationMastery">
              <el-checkbox-group v-model="form.communicationMastery">
                <el-checkbox
                  v-for="dict in dict.type.ya_communication_mastery"
                  :key="dict.value"
                  :label="dict.value">
                  {{dict.label}}
                </el-checkbox>
              </el-checkbox-group>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="title!='修改面试掌握情况记录'">提交反馈</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listInterview_mastery,
  getInterview_mastery,
  delInterview_mastery,
  addInterview_mastery,
  updateInterview_mastery
} from "@/api/teaching/interview_mastery";
import {listInterview_masteryDegree} from "@/api/teaching/interview";
import {listAllClasses} from "@/api/teaching/class";
import {listAllStudent} from "@/api/teaching/student";

export default {
  name: "Interview_mastery",
  dicts: ['sys_stage', 'ya_importance', 'ya_communication_mastery', 'ya_mastery_degree', 'ya_qualified', 'ya_summarized'],
  data() {
    var checkCommunicationInterview_mastery = (rule, value, callback) => {
      if (value.length == 0) {
        this.$modal.msgError("请填写沟通情况,在下一个Tab中");
        return callback(new Error('请填写沟通情况'));
      } else {
        callback();
      }
    };
    return {
      selectedInterviewList: [],
      studentList: [],
      classesList: [],
      // 高频面试题表格数据
      interview_masteryDegreeList: [],
      // 遮罩层
      loading: true,
      interview_masteryDegreeListLoading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 面试掌握情况记录表格数据
      interview_masteryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        communicationInterview_mastery: null,
        userId: null,
      },
      // 查询参数
      queryInterviewParams: {
        pageNum: 1,
        pageSize: 10,
        stage: null,
        qualified: null,
        question: null,
        answer: null,
        knowledgePoint: null,
        importance: null,
        onlyLast: false
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        communicationInterview_mastery: [
          {validator: checkCommunicationInterview_mastery, trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getClassesList();
  },
  methods: {
    checkDisabled(questionid, selectedValue) {
      let findElement = questionid + "," + 1;
      //是否选中无概念
      let target = this.selectedInterviewList.find(v => v.includes(findElement));
      if (target) {
        return target && selectedValue != 1;
      }
      //是否选中模糊
      findElement = questionid + "," + 2;
      target = this.selectedInterviewList.find(v => v.includes(findElement));
      if (target) {
        return target && selectedValue != 2;
      }
      return false;
    },
    changeStudentList(classesId) {
      this.queryParams.userId = null;
      listAllStudent(classesId).then(response => {
        this.studentList = response.rows;
      });
    },
    getClassesList() {
      listAllClasses().then(response => {
        this.classesList = response.rows;
      });
    },
    /** 查询面试掌握情况记录列表 */
    getMasteryDegreeList() {
      if (!this.queryInterviewParams.userId) {
        this.$modal.msgWarning("请依次选择[班级-学员]");
        return;
      }
      this.interview_masteryDegreeListLoading = true;
      listInterview_masteryDegree(this.queryInterviewParams).then(response => {
        //处理一下上次掌握情况
        for (let i = 0; i < response.rows.length; i++) {
          if (response.rows[i].hisDegree && response.rows[i].hisDegree.includes(",")) {
            response.rows[i].hisDegree = response.rows[i].hisDegree.split(",");
          }
        }
        this.interview_masteryDegreeList = response.rows;
        this.interviewTotal = response.total; // 新增总条数赋值
        this.interview_masteryDegreeListLoading = false;
      });
    },
    /** 查询面试掌握情况记录列表 */
    getList() {
      this.loading = true;
      listInterview_mastery(this.queryParams).then(response => {
        this.interview_masteryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.interview_masteryDegreeList = [];
      this.resetInterview_masteryDegreeQuery();
      this.selectedInterviewList = [];
      this.form = {
        id: null,
        communicationMastery: [],
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        classesId: null,
        userId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetInterview_masteryDegreeQuery() {
      this.queryInterviewParams = {
        pageNum: 1,
        pageSize: 10,
        importance: null,
        question: null,
        stage: null,
        classesId: null,
        userId: null,
        onlyLast: false,
        qualified: null
      };
      this.resetForm("queryInterviewForm");
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加面试掌握情况记录";
      this.interview_masteryDegreeListLoading = false;
      // this.getInterview_masteryDegreeList();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      this.interview_masteryDegreeListLoading = true;
      getInterview_mastery(id).then(response => {

        if (response.data.communicationInterview_mastery && response.data.communicationInterview_mastery.includes(",")) {
          response.data.communicationInterview_mastery = response.data.communicationInterview_mastery.split(",");
        }

        //处理一下上次掌握情况
        for (let i = 0; i < response.data.interview_masteryDetailList.length; i++) {
          if (response.data.interview_masteryDetailList[i].hisDegree && response.data.interview_masteryDetailList[i].hisDegree.includes(",")) {
            response.data.interview_masteryDetailList[i].hisDegree = response.data.interview_masteryDetailList[i].hisDegree.split(",");
          }
        }
        this.interview_masteryDegreeList = response.data.interview_masteryDetailList;
        this.form = response.data;
        this.open = true;
        this.title = "修改面试掌握情况记录";
        this.interview_masteryDegreeListLoading = false;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInterview_mastery(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            if(!this.queryInterviewParams.userId){
              this.$modal.msgWarning("选择一名学员");
              return;
            }
            if(this.selectedInterviewList.length===0){
              this.$modal.msgWarning("至少一条面试记录");
              return;
            }
            let submitForm = JSON.parse(JSON.stringify(this.form))
            submitForm.communicationMastery = this.form.communicationMastery.join(",");
            submitForm.userId = this.queryInterviewParams.userId;
            submitForm['selectedInterviewList'] = this.selectedInterviewList;
            addInterview_mastery(submitForm).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除面试掌握情况记录编号为"' + ids + '"的数据项？').then(function () {
        return delInterview_mastery(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('teaching/interview_mastery/export', {
        ...this.queryParams
      }, `interview_mastery_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
